/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: code_generator_utils.cpp
 *
 * Purpose: helper function implementation for code generator
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "code_generator_utils.h"
#include <fstream>
#include <sstream>

#include "log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define VAR_PREFIX "${"
#define VAR_SUFFIX "}"

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
bool ReadTemplateFile(const std::string& tpl_file, std::string& out_tpl)
{
    std::ifstream ifs(tpl_file);
    if (!ifs.is_open())
    {
        LOGE("open file: %s, failed\n", tpl_file.c_str());
        return false;
    }

    std::stringstream ss ; 
    ss << ifs.rdbuf() ; 
    out_tpl = std::move(ss.str()); 
    return true;
}

bool VarReplace(std::string& src, const std::string& var, const std::string& replace_var)
{
    if (var.empty())
    {
        return false;
    }

    std::string var_str = VAR_PREFIX + var + VAR_SUFFIX;
    
    size_t  pos = src.find(var_str);
    if (pos == std::string::npos)
    {
        return false;
    }

    src.replace(pos, var_str.length(), replace_var);
    return true;
}

bool VarReplaceAll(std::string& src, const std::string& var, const std::string& replace_var)
{
    if (var.empty())
    {
        return false;
    }
    
    std::string var_str = VAR_PREFIX + var + VAR_SUFFIX;

    size_t start_pos = 0;
    while((start_pos = src.find(var_str, start_pos)) != std::string::npos)
    {
        src.replace(start_pos, var_str.length(), replace_var);
        start_pos += replace_var.length(); // In case 'replace_var' contains 'var_str', like replacing 'x' with 'yx'
    }

    return true;

}



